package com.cavy.platform.server.domain.job;

import com.cavy.platform.server.domain.AgentOnline;
import com.cavy.platform.server.domain.SchedulingStrategy;

import java.util.Collection;
import java.util.Random;

/**
 * @author 唐旭
 * @Date 2018/7/25
 */
public class RandomJobSelector implements JobDispatcher {
    @Override
    public boolean support(SchedulingStrategy schedulingStrategy) {
        return SchedulingStrategy.RANDOM.equals(schedulingStrategy);
    }

    @Override
    public AgentOnline select(Collection<AgentOnline> list) {
        int max = list.size() - 1;
        int i = 0;
        if (max != 0) {
            Random random = new Random();
            i = random.nextInt(max);
        }
        return (AgentOnline) list.toArray()[i];
    }
}
